using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using bitzhuwei.Compiler;

namespace bitzhuwei.AnsiCFormat {
    /// <summary>
    /// Correspond to the Vn node identifier_list in the grammar(AnsiC).
    /// </summary>
    internal partial class Vnidentifier_list : IFullFormatter {
        // [151]=identifier_list : 'identifier' ;
        // [152]=identifier_list : identifier_list ',' 'identifier' ;


        private readonly Token first0;
        private readonly List<Token> list0 = new();
        private readonly List<Token> list1 = new();
        private readonly TokenRange _scope;

        public TokenRange Scope => _scope;

        public void Format(TextWriter writer, FormatContext context) {
            throw new NotImplementedException();
        }

        public void FullFormat(BlankConfig preConfig, TextWriter writer, FormatContext context) {
            {
                context.PrintBlanksAnd(this.first0, preConfig, writer);
            }
            var first = true;
            var config = new BlankConfig(inlineBlank: 1, forceNewline: false);
            for (var i = 0; i < this.list0.Count; i++) {
                var item0 = this.list0[i];
                if (first) {
                    context.PrintCommentsBetween(this.first0, item0, config, writer);
                    first = false;
                }
                else {
                    context.PrintCommentsBetween(this.list0[i - 1], item0, config, writer);
                }
                context.PrintBlanksAnd(item0, config, writer);
                var item1 = this.list1[i];
                context.PrintCommentsBetween(item0, item1, config, writer);
                context.PrintBlanksAnd(item1, config, writer);
            }
        }

        public IEnumerable<string> YieldTokens(TextWriter writer, FormatContext context) {
            throw new NotImplementedException();
        }
        // [151]=identifier_list : 'identifier' ;
        // dumped by ListExtractor 1
        internal Vnidentifier_list(Token first0) {
            this.first0 = first0;
            this._scope = new TokenRange(first0);
        }
        // [152]=identifier_list : identifier_list ',' 'identifier' ;
        // dumped by ListExtractor 2
        internal void Add(
            Token r1/* reserved word is omissible */
            , Token r0
        ) {
            this.list0.Add(r1);
            this.list1.Add(r0);
            this._scope.end = r0.index;
        }

    }
}
